2
2
.
.
5
5
.
.
4
4
I
I
n
n
l
l
i
i
n
n
e
e
F
F
u
u
n
n
c
c
t
t
i
i
o
o
n
n
s
s
I
I
n
n
f
f
o
o
[
[
R
R
]
]
[
[
R
R
]
]
Higher-order functions either (all other functions are first-order functions)
accept function as argument
return a function
Higher order functions are stored as Objects (which uses memory).
When declaring Function, use inline keyword to tell compiler to simply copy/inject function code at the calling place.
Inline functions must be named and can't be declared inside another function (only as Methods or top level Functions).
If inline Function has Input Parameter that accepts function, then provided Lambda expression can have basic return.
This will exit the function inside which inlined function was called (because it was copied inside it).
Such Lambda expression needs to be given at the place of Input Parameter so that Compiler knows that Lambda
Expression will be used to call Inline Function and that it can therefore support simple return.
You can't store such Lambda Expression into a Variable if it has simple return.
Lambda Expression with simple return
//===========================================================================================================
// FUNCTION: executeFunction
//===========================================================================================================
inline fun executeFunction(receivedFunction: (name: String, age: Int) -> Unit) {
var result = receivedFunction("John", 20) //return
println("Not executed")
}
//===========================================================================================================
// FUNCTION: main
//===========================================================================================================
fun main() {
executeFunction({ name: String, age: Int -> print("$name IS $age YEARS OLD."); return }) //return
println("Not executed")
}
Output
John IS 20 YEARS OLD.